package com.octopus.registry.test;

import static org.junit.Assert.*;

import org.junit.Test;

import com.octopus.json.Json;
import com.octopus.json.JsonUtils;
import com.octopus.registry.Call;
import com.octopus.registry.MethodIsNullException;
import com.octopus.registry.ServiceDoesNotExistException;
import com.octopus.registry.ServiceInvokeNullParameterException;
import com.octopus.registry.ServiceRegistry;
import com.octopus.registry.ServiceUtils;

public class ServiceTestCase {

	@Test
	public void test() {
		ServiceRegistry sr = new ServiceRegistry();
		try {
			sr.register(TestService.class);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} 
		System.out.println(sr.toServiceXMLString());
		
		Json json = JsonUtils.createJsonObject();
		try {
			sr.invokeService("com.octopus.registry.test.TestService", "Test2", json);
		} catch (ServiceInvokeNullParameterException e) {
			e.printStackTrace();
		} catch (ServiceDoesNotExistException e) {
			e.printStackTrace();
		} catch (MethodIsNullException e) {
			e.printStackTrace();
		}
		Json result = null;
		result = sr.invokeServiceSimple("com.octopus.registry.test.TestService", "Test2", json);
		Call call1 = ServiceUtils.getCallFromResultJson(json, result);
		assertTrue(call1.isSuccess());
		json.cleanReservedMap();
		result = sr.invokeServiceSimple("com.octopus.registry.test.TestService", "Test3", json);
		Call call2 = ServiceUtils.getCallFromResultJson(json, result);
		assertTrue(call2.isSuccess());
		json.cleanReservedMap();
		result = sr.invokeServiceSimple("com.octopus.registry.test.TestService", "Test4", json);
		Call call3 = ServiceUtils.getCallFromResultJson(json, result);
		assertTrue(call3.isSuccess());
		json.cleanReservedMap();
		result = sr.invokeServiceSimple("com.octopus.registry.test.TestService", "Test5", json);
		Call call4 = ServiceUtils.getCallFromResultJson(json, result);
		assertNotNull(call4);
		assertTrue(!call4.isSuccess());
		assertTrue(call4.isInvokeException());
		json.cleanReservedMap();
		assertEquals(TestException.class, call4.getRealCause().getClass());
	}

}
